# import json
#
# from openai import OpenAI
# from baidusearch.baidusearch import search
#
# # from audio import Audio, Speaker
#
# from utils.utils import account_config
# config = account_config()
# api_key, base_url, model = config['api_key'], config['base_url'], config['model']
#
# client = OpenAI(api_key=api_key, base_url=base_url)
#
#
# class Func:
#
#     @staticmethod
#     def calc_sum(a: int, b: int) -> int:
#         print(f"exec calc_sum")
#         return a + b
#
#     @staticmethod
#     def search_web(query_context: str) -> str:
#         result = search(query_context, num_results=20)
#         result = "\n".join([f"可能得结果：{r['title']}" for r in result])
#         return result
#
#
# functions = [
#     {
#         "type": "function",
#         "function": {
#             "name": "calc_sum",
#             "description": "计算两数之和",
#             "parameters": {
#                 "type": "object",
#                 "properties": {
#                     "a": {
#                         "type": "number",
#                         "description": "求和的第一个数"
#                     },
#                     "b": {
#                         "type": "number",
#                         "description": "求和的第二个数"
#                     }
#                 },
#                 "required": ["a", "b"],
#                 "additionalProperties": False
#             }
#         }
#     },
#     {
#         "type": "function",
#         "function": {
#             "name": "search_web",
#             "description": "用网络搜索引擎获取最新信息",
#             "parameters": {
#                 "type": "object",
#                 "properties": {
#                     "query_context": {
#                         "type": "string",
#                         "description": "要搜索的关键词"
#                     }
#                 },
#                 "required": ["query_context"],
#                 "additionalProperties": False
#             }
#         }
#     }
# ]
#
#
# class Agent:
#
#     def __init__(self, messages, functions):
#         self.messages = messages
#         self.functions = functions
#
#     def send_ask(self):
#         response = client.chat.completions.create(
#             model=model,
#             messages=self.messages,
#             # tools=self.functions,
#             # tool_choice="auto",
#             stream=False
#         )
#         return response
#
#     def ask(self, content=None, p_res=None):
#
#         if content and not p_res:
#             self.messages += [{"role": "user", "content": content}]
#             response = self.send_ask()
#         else:
#             response = p_res
#
#         for res in response.choices:
#             finish_reason = res.finish_reason
#             if finish_reason == "tool_calls":
#                 tool_calls = res.message.tool_calls
#                 for function_info in tool_calls:
#                     func_obj_name = function_info.function.name
#                     args = function_info.function.arguments
#                     self.messages.append({"role": "assistant", "tool_calls": [{"id": function_info.id, "function": {"name": func_obj_name, "arguments": args}, "type": "function"}]})
#                     func_result = getattr(Func, func_obj_name)(**json.loads(args))
#                     self.messages.append({"role": "tool", "content": json.dumps({"result": func_result}), "tool_call_id": function_info.id})
#                 self.ask(content=None, p_res=self.send_ask())
#             elif finish_reason == "stop":
#                 print(res.message.content)
#                 self.messages.append({"role": res.message.role, "content": res.message.content})
#                 return res.message.content
#
#         return None
#
#
# messages = [
#     {
#         "role": "system",
#         "content": """
#              你是一个助手。
#              当你调用了工具函数（tool/functions）之后，可以进行自然语言润色，但是不需要判断结果是否合理。
#              如果用户问题涉及实时信息或新闻，请调用 search_web 工具搜索最新信息，而不是凭空编造。
#              """
#     }
# ]
# agent = Agent(messages, functions)
# # audio = Audio()
#
# # while True:
# #     # input_data = audio.recognize()
# #     input_data = input("请输入:")
# #     if input_data:
# #         text = agent.ask("".join(input_data))
# #         # Speaker().speak(text)
# #     else:
# #         # Speaker().speak("没有听到声音")
# #         print("<UNK>")
